package modules.dimensioning.solution;

import java.util.LinkedList;

import org.geotools.geometry.jts.JTSFactoryFinder;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

import datatypes.Antenna;
import datatypes.TerrainType;

public class PartialAntennaArray {

	public double fromX;
	public double fromY;
	public double toX;
	public double toY;

	public Geometry g;
	
	public TerrainType tp;
	
	public PartialAntennaArray(Geometry g, TerrainType tp) {
		this.g = g;
		this.tp = tp;
	}
	public boolean isBetween(Antenna a) {
		boolean x = (a.getX() >= fromX && a.getX() <= toX);
		boolean y = (a.getY() >= fromY && a.getY() <= toY);
		return (x && y);
	}
	
	public Geometry getPartialGeometry() {
		com.vividsolutions.jts.geom.GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );			
		WKTReader reader = new WKTReader( geometryFactory );	
		Polygon area = null;
		try {
			area = (Polygon) reader.read("POLYGON ((" 
					+ (fromX-50) + " " + (fromY-50) + ","
					+ (toX+50) + " " + (fromY-50) + ","
					+ (toX+50) + " " + (toY+50) + ","
					+ (fromX-50) + " " + (toY+50) + ","
					+ (fromX-50) + " " + (fromY-50) 
					+ "))");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return area.intersection(g);					
	}
	
	public LinkedList<Antenna> getList() {
		// TODO Auto-generated method stub
		return list;
	}


	public String toString() {
		return "PS: X:" + fromX + "->" + toX + " ; Y:" + fromY + "->" + toY + " ; Size: " + list.size() + " ; " + getPartialGeometry().toString();
	}
	
	LinkedList<Antenna> list = new LinkedList<Antenna>();
	
	public boolean addAntenna(Antenna a) {
		if (isBetween(a)) {
			a.setTerrainType(tp);
			list.add(a);
		}
		return isBetween(a);
	}
	
}